package com.contactbook.web.server.service;

import com.contactbook.web.client.dto.AccountDTO;
import com.contactbook.web.client.service.AccountService;
import com.contactbook.web.server.dto.AccountDTOConverter;

import contactbook.model.Account;
import contactbook.service.AccountServiceInterface;

public class AccountServiceImpl implements AccountService {

	private AccountServiceInterface accountService;
	
	public AccountServiceImpl() {
		ServiceLocator sl = ServiceLocator.getInstance();
		accountService = sl.getAccountService();
	}
	
	@Override
	public AccountDTO login(String login, String password) {
		Account account = accountService.login(login, password);
		if (account != null) {
			AccountDTO accountDTO = AccountDTOConverter.convert(account, true);
			return accountDTO;
		}
		return null;
	}

	@Override
	public void logout() {
		accountService.logout();
	}

	@Override
	public AccountDTO create(AccountDTO accountDTO) {
		Account account = new Account();
		account.setId(accountDTO.getId());
		account.setLogin(accountDTO.getLogin());
		account.setPassword(accountDTO.getPassword());
		
		Account a = accountService.create(account);
		
		return AccountDTOConverter.convert(a, true);
	}

	@Override
	public void delete() {
		accountService.delete();
	}

	@Override
	public void update(AccountDTO accountDTO, String login, String password) {
		Account account = accountService.getAccountById(accountDTO.getId());
		account.setLogin(accountDTO.getLogin());
		account.setPassword(accountDTO.getPassword());
		accountService.update(account);
	}
	
}
